<?php
class Devilslider extends Module
{

	public $messages = array();
	
	
	
	function __construct()
	{
		$this->name = 'devilslider';
		$this->tab = 'Blocks';
		$this->version = 1.0;

		parent::__construct(); // The parent construct is required for translations

		$this->page = basename(__FILE__, '.php');
		$this->displayName = $this->l('Devil Slider');
		$this->description = $this->l('Adds the slider for images');
		
		
	}

	
	
	
	function install()
	{
		for($s=0; $s<5; $s++){	
			Configuration::updateValue('DEVIL_IMG'.$s.'S_EXT','jpg');		
			
		}
		if (!parent::install())
			return false;
		if (!$this->registerHook('home'))
			return false;
		return true;
	}

	
	function hookHome($params){
	
		//print_r($_GLOBALS);
		$arr = array();
		for($i = 0; $i<5; $i++){
			$url = 'img'.$i.'.' . Configuration::get('DEVIL_IMG'.$i.'S_EXT');
			$name = "Image ".($i+1);
			$arr[$name] =  $url;
			//array_push($arr, );
		}
	
		global $smarty;
		$smarty->assign('images', $arr);
		//print_r($arr);
		//echo "Hello";
		return $this->display(__FILE__, 'devilslider.tpl');
	}
	
	
	
	
	
	private function _deleteCurrentImg($imgname)
	{
		if (file_exists(_PS_MODULE_DIR_.$this->name.'/'.$imgname.'.'.Configuration::get('SAMPLE_'.$imgname.'_EXT')))
			unlink(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('SAMPLE_'.$imgname.'_EXT'));
			
		
	}
	
	public function upload($img,$index){
		//print_r( $_FILES);
		$errors = '';
		$ext = 'png';
		if ($dot_pos = strrpos($_FILES[$img]['name'],'.')){			
			$ext = substr($_FILES[$img]['name'], $dot_pos+1);
			
			if (!move_uploaded_file($_FILES[$img]['tmp_name'],_PS_MODULE_DIR_.$this->name.'/img/'.$img.'.'.$ext))
				$this->messages[$index] = "<font color=\"red\">Error on uploading</font>";				
			else
				$this->messages[$index] = "<font color=\"green\">Uploaded</font>";				
		}else{
			$this->messages[$index] = "<p color=\"red\">Error on uploading</p>";
		}
		
		echo "<Br>$index Extenstion:$ext <br>";
		Configuration::updateValue('DEVIL_IMG'.$index.'S_EXT', $ext);
		echo '<br> Image: '.$img. "---- ". Configuration::get('DEVIL_IMG'.$index.'S_EXT');
		
	}
	
	
	
	public function postProcess(){
	
		//print_r($_FILES);
		
		for($i = 0; $i<5; $i++){
			if(isset($_FILES['img'.$i]) AND $_FILES['img'.$i]['size'] > 0){				
				$this->upload('img'.$i,$i);				
				break;
			}
		}
		
	
	}
	
	public function getContent()
	{	
		
		$this->postProcess();
		$html = '<h2>Image Slider</h2>
		<form action="'.$_SERVER['REQUEST_URI'].'" method="post" enctype="multipart/form-data">';
			for($i = 0; $i<5; $i++){
			$html .= '<p>
				<label>'. ($i+1) .'. Image </label><input type="file" name="img'. $i .'" /><input type="submit" value="Upload" /> '.$this->messages[$i].'
			</p>';
			}
			
		$html .= '</form>';
		return $html;
	}
	
	

}
?>